/**
 * Created by xuweijie on 2017/5/19.
 */
//layer弹出层
layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    //触发事件
    var active={
        delete: function (othis) {
            //删除
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/delmybuycar.action",
                        type: 'post',
                        data: {
                            buycarid: othis.buycarid,
                            del_flag:"1"
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == "0") {
                                layer.msg("删除成功");
                                window.location.href = '/front/mybuycar/buycarlist';
                            }else {
                                layer.msg("删除失败");
                            }
                        }
                    });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        },
        update: function (othis) {
            var content = $('#updatebuycar');
            layer.open({
                type: 1,
                title:'修改购买信息',
                skin: 'layui-layer-molv', //样式类名
                closeBtn: false, //不显示关闭按钮
                anim: 2,
                area: '650px;',
                btn: ['绝不后悔', '我怂了'],
                shadeClose: true, //开启遮罩关闭
                content: content,
                success: function (layero) {
                    var roleInfoParent = $(othis).parents('tr');
                    var roleInfoItem = roleInfoParent.children("td");
                    $('#goodname').val(roleInfoItem[1].innerHTML);
                    $('#numbers').val(roleInfoItem[6].innerHTML);
                },
                yes: function (index, layero) {
                    //先序列化所有参数，再通过ajax提交数据
                    var params = $.param({
                        'buycarid':othis.buycarid,
                        'numbers': $('#numbers').val()
                    }, true);
                    $.ajax({
                        url: '/updatemybuycar.action',
                        type: 'post',
                        data: params,
                        dataType: 'json',
                        success: function (data) {
                            if (data === "0") {
                                layer.msg("更新成功");
                                window.location.href = '/front/mybuycar/buycarlist';
                            }else {
                                layer.msg("更新失败");
                            }
                        }
                    });
                }
            });
        }
    };
    $('.delbuycar').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.buycarid = $(this).attr('buycarid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.updatebuycar').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.buycarid = $(this).attr('buycarid');
        active[method] ? active[method].call(this, othis) : '';
    });
});

//分页
layui.use(['laypage', 'layer'], function() {
    var laypage = layui.laypage, layer = layui.layer;
    //地址分页
    laypage({
        cont: 'buycarpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function(obj, first){
            //得到了当前页，用于向服务端请求对应数据
            if(first!=true){//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                window.location.href ="/front/mybuycar/buycarlist?pageIndex="+pageIndex;
            }
        }
    });
});

$(function () {
   $('.jiesuan').click(function () {
       var numprice=$(this).attr('numprice');
       var purchaseid=$(this).attr('purchaseid');
       var params = $.param({
           numprice: numprice,
           purchaseid:purchaseid
       }, true);
       $.ajax({
           url:'',
           type:'post',
           data:params,
           dateType: 'json',
           success:function (){
               window.location.href ='/front/submitorder/submitorderlist?numprice='+numprice+'&purchaseid='+purchaseid;
           }
       });
   })
});